import { EditableTableColumn } from '@/components/table';
// import { DataSource, ValueType, columnRename, getDbTypeOptions, query, queryForMap } from "@/services";
import { ConnectorDataSource, DataSource, ValueType, query, queryForMap } from "@/services";

export const mysqlColumns = async (): Promise<EditableTableColumn[]> => {
    //获取到表结构
    const columns: EditableTableColumn[] = [
        {
            name: 'ordinal_position',
        },
        {
            name: 'column_name',
        },
        {
            name: 'column_default',
        },
        {
            name: 'is_nullable',
        },
        {
            name: 'data_type',
            valueType: ValueType.String,
            valueLength: 0,
            selector: {
                widget: ['select']
            },
            rwSame: true,
            // options: {
            //     items: getDbTypeOptions('MySQL')
            // }
        },
        {
            name: 'column_type',
        },
        {
            name: 'column_key',
        },
        {
            name: 'Extra',
        }
    ];
    return columns;
};


export const mysqlTableRequest = async (dataSource: ConnectorDataSource, table: string): Promise<Array<any>> => {
    let data = await queryForMap(dataSource, `select c.ordinal_position, c.column_name,(
        CASE
        WHEN c.column_default is NULL  THEN '' 
        ELSE c.column_default 
        END
    ) AS column_default, c.is_nullable, c.data_type,c.column_type,c.column_key,c.extra  from information_schema.COLUMNS c
        WHERE c.TABLE_SCHEMA = database() and c.table_name = ?  order by c.ordinal_position`, [table], {})
    console.log(data);
    return data;
};

export const mysqlAlterTable = async (dataSource: ConnectorDataSource, table: string, rowIndex: number, columnId: string, value: unknown, data: { [key: string]: any }) => {
    if (columnId === 'name') {
        // await columnRename(dataSource, table, data['rawValue'], value as string)
    }
}
